﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class Post : System.Web.UI.Page
    {
        baivietBUS bv = new baivietBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDDl();
            }
            btncapnhat.Visible = false;
            lblError.Visible = false;
            lblSuccess.Visible = false;
            successbox.Visible = false;
            errorbox.Visible = false;
        }
        private void LoadDDl()
        {
            string sql = "SELECT * FROM tblGroupConfig";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            ddlGroup.DataSource = table;
            ddlGroup.DataTextField = "groupName";
            ddlGroup.DataValueField = "idGroup";
            ddlGroup.DataBind();
            dtbaiviet.DataSource = bv.getallconfig().DefaultView;
            dtbaiviet.DataBind();

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text.Trim() == "")
            {
                lblError.Visible = true;
                errorbox.Visible = true;
                txtTitle.Focus();
                lblError.Text = "Bạn chưa nhập tiêu đề bài viết";
            }
            else if (ddlGroup.SelectedIndex == 0)
            {
                lblError.Visible = true;
                errorbox.Visible = true;
                ddlGroup.Focus();
                lblError.Text = "Bạn chưa chọn nhóm hiển thị của bài viết";
            }
            else if (ddlIsShow.SelectedIndex == 0)
            {
                lblError.Visible = true;
                errorbox.Visible = true;
                ddlIsShow.Focus();
                lblError.Text = "Bạn chưa chọn nhóm hiển thị";
            }
            else
            {
                #region Save
                if (fUlIcon.HasFile)
                {
                    string Title = txtTitle.Text.Trim();
                    string fileName = fUlIcon.PostedFile.FileName;
                    string path = Server.MapPath("../../../images/images/");
                    fUlIcon.SaveAs(path + fileName);
                    string Desc = txtDescription.Text;
                    string Content = txtContent.Text;
                    string idGroup = ddlGroup.SelectedValue.ToString();
                    string key = GenerateRandomPassword(6);
                    string isShow = ddlIsShow.SelectedValue.ToString();
                    string today = DateTime.Now.ToShortDateString();
                    string link = txtLink.Text;
                    string tag = txtTags.Text;
                    if (link.Length <= 0)
                    {
                        if (idGroup != "blog")
                        {
                            link = GenerateURL(ConvertTitle(Title), key).ToString();
                        }
                        else
                        {
                            link = GenerateURL(ConvertTitle(Title), key).ToString();
                            link = link.Replace(".html", ".blog-post");
                        }
                    }
                    if (ConnectionData._MyConnection.State == ConnectionState.Closed)
                    {
                        ConnectionData._MyConnection.Open();
                    }
                    string sql = "Insert Into tblConfig ([key],keyName,keyDescription,keyImage,value,tag,link,idGroup,isShow,createDate) values (@key,@keyName,@keyDescription,@keyImage,@value,@tag,@link,@idGroup,@isShow,@createDate) ";
                    SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@key", SqlDbType.NVarChar).Value = key;
                    cmd.Parameters.Add("@keyName", SqlDbType.NVarChar).Value = Title;
                    cmd.Parameters.Add("@keyDescription", SqlDbType.NVarChar).Value = Desc;
                    cmd.Parameters.Add("@keyImage", SqlDbType.NVarChar).Value = "/images/" + fileName;
                    cmd.Parameters.Add("@value", SqlDbType.NVarChar).Value = Content;
                    cmd.Parameters.Add("@tag", SqlDbType.NVarChar).Value = tag;
                    cmd.Parameters.Add("@link", SqlDbType.NVarChar).Value = link;
                    cmd.Parameters.Add("@idGroup", SqlDbType.NVarChar).Value = idGroup;
                    cmd.Parameters.Add("isShow", SqlDbType.Bit).Value = isShow;
                    cmd.Parameters.Add("@createDate", SqlDbType.Date).Value = today;
                    cmd.ExecuteNonQuery();
                    LoadDDl();
                    Success();
                }
                else
                {
                    string Title = txtTitle.Text.Trim();
                    string Desc = txtDescription.Text;
                    string Content = txtContent.Text;
                    string idGroup = ddlGroup.SelectedValue.ToString();
                    string key = GenerateRandomPassword(6);
                    string today = DateTime.Now.ToShortDateString();
                    string isShow = ddlIsShow.SelectedValue.ToString();
                    string link = txtLink.Text;
                    string tag = txtTags.Text;
                    if (link.Length <= 0)
                    {
                        if (idGroup != "blog")
                        {
                            link = GenerateURL(ConvertTitle(Title), key).ToString();
                        }
                        else
                        {
                            link = GenerateURL(ConvertTitle(Title), key).ToString();
                            link = link.Replace(".html", ".blog-post");
                        }
                    }
                    if (ConnectionData._MyConnection.State == ConnectionState.Closed)
                    {
                        ConnectionData._MyConnection.Open();
                    }
                    string sql = "Insert Into tblConfig ([key],keyName,keyDescription,value,tag,link,idGroup,isShow,createDate) values (@key,@keyName,@keyDescription,@value,@tag,@link,@idGroup,@isShow,@createDate) ";
                    SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@key", SqlDbType.NVarChar).Value = key;
                    cmd.Parameters.Add("@keyName", SqlDbType.NVarChar).Value = Title;
                    cmd.Parameters.Add("@keyDescription", SqlDbType.NVarChar).Value = Desc;
                    cmd.Parameters.Add("@value", SqlDbType.NVarChar).Value = Content;
                    cmd.Parameters.Add("@tag", SqlDbType.NVarChar).Value = tag;
                    cmd.Parameters.Add("@link", SqlDbType.NVarChar).Value = link;
                    cmd.Parameters.Add("@idGroup", SqlDbType.NVarChar).Value = idGroup;
                    cmd.Parameters.Add("isShow", SqlDbType.Bit).Value = isShow;
                    cmd.Parameters.Add("@createDate", SqlDbType.Date).Value = today;
                    cmd.ExecuteNonQuery();
                    LoadDDl();
                    Success();
                }
                #endregion
            }
        }
        public static string GenerateRandomPassword(int length)
        {
            string allowedLetterChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
            string allowedNumberChars = "0123456789";
            char[] chars = new char[length];
            Random rd = new Random();
            bool useLetter = true;
            for (int i = 0; i < length; i++)
            {
                if (useLetter)
                {
                    chars[i] = allowedLetterChars[rd.Next(0, allowedLetterChars.Length)];
                    useLetter = false;
                }
                else
                {
                    chars[i] = allowedNumberChars[rd.Next(0, allowedNumberChars.Length)];
                    useLetter = true;
                }
            }
            return new string(chars);
        }
        public static string ConvertTitle(object Title)
        {
            string stTitle = Title.ToString();
            stTitle = ConvertVN(stTitle).ToLower();
            stTitle = stTitle.Replace(" ", "-");
            stTitle = stTitle.Replace(":", "");
            stTitle = stTitle.Replace(",", "");
            return stTitle;
        }
        public static string ConvertVN(string chucodau)
        {
            const string FindText = "áàảãạâấầẩẫậăắằẳẵặđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵÁÀẢÃẠÂẤẦẨẪẬĂẮẰẲẴẶĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ";
            const string ReplText = "aaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyaaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyy";
            int index = -1;
            char[] arrChar = FindText.ToCharArray();
            while ((index = chucodau.IndexOfAny(arrChar)) != -1)
            {
                int index2 = FindText.IndexOf(chucodau[index]);
                chucodau = chucodau.Replace(chucodau[index], ReplText[index2]);
            }
            return chucodau;
        }
        public static string GenerateURL(string Title, string strId)
        {
            string strTitle = Title.ToString();
            #region Generate SEO Friendly URL based on Title
            //Trim Start and End Spaces.
            strTitle = strTitle.Trim();
            //Trim "-" Hyphen
            strTitle = strTitle.Trim('-');
            strTitle = strTitle.ToLower();
            char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
            strTitle = strTitle.Replace("c#", "C-Sharp");
            strTitle = strTitle.Replace("vb.net", "VB-Net");
            strTitle = strTitle.Replace("asp.net", "Asp-Net");
            //Replace . with - hyphen
            strTitle = strTitle.Replace(".", "-");
            //Replace Special-Characters
            for (int i = 0; i < chars.Length; i++)
            {
                string strChar = chars.GetValue(i).ToString();
                if (strTitle.Contains(strChar))
                {
                    strTitle = strTitle.Replace(strChar, string.Empty);
                }
            }
            //Replace all spaces with one "-" hyphen
            strTitle = strTitle.Replace(" ", "-");
            //Replace multiple "-" hyphen with single "-" hyphen.
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("-----", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("--", "-");
            //Run the code again...
            //Trim Start and End Spaces.
            strTitle = strTitle.Trim();
            //Trim "-" Hyphen
            strTitle = strTitle.Trim('-');
            #endregion
            //Append ID at the end of SEO Friendly URL
            strTitle = strTitle + "-" + strId + ".html";
            return strTitle;
        }
        protected void btnEdit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = sender as ImageButton;
            string id = btn.CommandArgument;
            //Response.Redirect("Post.aspx?key=" + id);
            DataTable table = bv.getbykey(id);
            if (table.Rows.Count > 0)
            {
                txtTitle.Text = table.Rows[0]["keyName"].ToString();
                txtLink.Text = table.Rows[0]["link"].ToString();
                txtDescription.Text = table.Rows[0]["keyDescription"].ToString();
                txtContent.Text = table.Rows[0]["value"].ToString();
                Label1.Text = table.Rows[0]["key"].ToString();
                ddlGroup.SelectedValue = table.Rows[0]["idGroup"].ToString();
                ddlIsShow.SelectedValue = table.Rows[0]["isShow"].ToString();
                txtTags.Text = table.Rows[0]["tag"].ToString();
            }
            btnSubmit.Visible = false;
            btncapnhat.Visible = true;
        }
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            string key = ((ImageButton)sender).CommandArgument.ToString();
            int i = 0;
            try
            {
                if (ConnectionData._MyConnection.State == ConnectionState.Closed)
                {
                    ConnectionData._MyConnection.Open();
                }
                SqlCommand cmd = new SqlCommand("DELETE FROM tblconfig WHERE [key] =@key",
                    ConnectionData._MyConnection);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@key", SqlDbType.NVarChar).Value = key;
                i = cmd.ExecuteNonQuery();
                cmd.Dispose();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                ConnectionData._MyConnection.Close();
            }
            //return i > 0;
            LoadDDl();

        }
        protected void btncapnhat_Click(object sender, EventArgs e)
        {
            if (fUlIcon.HasFile)
            {
                string Title = txtTitle.Text.Trim();
                string fileName = fUlIcon.PostedFile.FileName;
                string path = Server.MapPath("../../../images/");
                fUlIcon.SaveAs(path + fileName);
                string Desc = txtDescription.Text;
                string Content = txtContent.Text;
                string idGroup = ddlGroup.SelectedValue.ToString();
                string key = Label1.Text;
                string today = DateTime.Now.ToShortDateString();
                string isShow = ddlIsShow.SelectedValue.ToString();
                string link = txtLink.Text;
                string tag = txtTags.Text;
                if (link.Length <= 0)
                {
                    link = GenerateURL(ConvertTitle(Title), key).ToString();
                }
                string sql = "update tblConfig set keyName=@keyName,keyDescription=@keyDescription,value=@value,tag=@tag,link=@link,idGroup=@idGroup,isShow=@isShow where [key]=@key";
                SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@key", SqlDbType.NVarChar).Value = key;
                cmd.Parameters.Add("@keyName", SqlDbType.NVarChar).Value = Title;
                cmd.Parameters.Add("@keyDescription", SqlDbType.NVarChar).Value = Desc;
                cmd.Parameters.Add("@keyImage", SqlDbType.NVarChar).Value = "/images/" + fileName;
                cmd.Parameters.Add("@value", SqlDbType.NVarChar).Value = Content;
                cmd.Parameters.Add("@tag", SqlDbType.NVarChar).Value = tag;
                cmd.Parameters.Add("@link", SqlDbType.NVarChar).Value = link;
                cmd.Parameters.Add("@idGroup", SqlDbType.NVarChar).Value = idGroup;
                cmd.Parameters.Add("@isShow", SqlDbType.Bit).Value = isShow;
                cmd.Parameters.Add("@createDate", SqlDbType.Date).Value = today;
                cmd.ExecuteNonQuery();
                LoadDDl();
                Success();
            }
            else
            {
                string Title = txtTitle.Text.Trim();
                string Desc = txtDescription.Text;
                string Content = txtContent.Text;
                string idGroup = ddlGroup.SelectedValue.ToString();
                string key = Label1.Text;
                string today = DateTime.Now.ToShortDateString();
                string isShow = ddlIsShow.SelectedValue.ToString();
                string link = txtLink.Text;
                string tag = txtTags.Text;
                if (link.Length <= 0)
                {
                    link = GenerateURL(ConvertTitle(Title), key).ToString();
                }
                string sql = "update tblConfig set keyName=@keyName,keyDescription=@keyDescription,value=@value,tag=@tag,link=@link,idGroup=@idGroup,isShow=@isShow where [key]=@key";
                SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@key", SqlDbType.NVarChar).Value = key;
                cmd.Parameters.Add("@keyName", SqlDbType.NVarChar).Value = Title;
                cmd.Parameters.Add("@keyDescription", SqlDbType.NVarChar).Value = Desc;
                cmd.Parameters.Add("@value", SqlDbType.NVarChar).Value = Content;
                cmd.Parameters.Add("@tag", SqlDbType.NVarChar).Value = tag;
                cmd.Parameters.Add("@link", SqlDbType.NVarChar).Value = link;
                cmd.Parameters.Add("@idGroup", SqlDbType.NVarChar).Value = idGroup;
                cmd.Parameters.Add("@isShow", SqlDbType.Bit).Value = isShow;
                cmd.Parameters.Add("@createDate", SqlDbType.Date).Value = today;
                cmd.ExecuteNonQuery();
                LoadDDl();
                Success();
            }
        }
        private void Success()
        {
            txtTitle.Text = "";
            txtLink.Text = "";
            ddlIsShow.SelectedIndex = 0;
            ddlGroup.SelectedIndex = 0;
            txtDescription.Text = "";
            txtContent.Text = "";
            successbox.Visible = true;
            lblSuccess.Visible = true;
            lblSuccess.Text = "Lưu bài viết thành công !!!";
        }
    }
}